--[[------------------------------------
			APEX FRAMEWORK
	Developed by Ren, from zDark.com
------------------------------------]]--

-- Main database loaded when server begins.
function Initialize()
	DBHandler()
end
hook.Add("Initialize", "Initialize", Initialize)

-- Database handler, table creation and checker.
function DBHandler()
	
		-- Only run once.
		if (sql.TableExists("apex")) then Msg("\nApex Database Exists!\n")
		else
				if (!sql.TableExists("apex")) then
					
						-- Inject table values.
						sql.Query("CREATE TABLE apex (sid varchar(255), credits int, model varchar(255), name varchar(255), age int, job varchar(255) )")
					
							-- Error Handling
							if (sql.TableExists("apex")) then 
								 Msg("\nApex Database Created!\n")
				
							else Msg("\nApex Database Creation Error!\n")
								 Msg(sql.LastError(result) .. "\n" )
							end
				end
		end
end

-- Player initial events.
function PlayerInitialSpawn(pl)
	
	-- Player db checker.
	local sid = pl:SteamID()
	pl:SetNWString("sid", sid)
	ExistingPlayer(pl)
	
	-- Timers
	timer.Create("Save", 5, 0, function() Save(pl) end)

end
hook.Add("PlayerInitialSpawn", "PlayerInitialSpawn", PlayerInitialSpawn)

-- Existing player checker.
function ExistingPlayer(pl)
		
	-- Variable declairation.
	local sid = pl:GetNWString("sid")
	local query = "SELECT sid FROM apex WHERE sid = '"..sid.."'"
	local result = sql.Query(query)
		
		-- Player db checker.
		if (result) then
			ApexSQL(pl)
		else
			NewPlayer(sid, pl)
		end
end

-- New player creation, table injection.
function NewPlayer(sid, pl)
	Msg("New Player\n")
	-- Variable declairation.
		Msg(sid.."\n")
	local name = pl:Nick()
		Msg(name.."\n")
		-- Inject default values
		sql.Query("INSERT INTO apex (`sid`, `credits`, `model`, `name`, `age`, `job`) VALUES ('"..sid.."', '100', 'models/player/Group01/male_01.mdl', '"..name.."', '13', '1')")
	
	local result = sql.Query("SELECT sid, credits, model, name, age, job FROM apex WHERE sid = '"..sid.."'")
		Msg(result)
		
		-- Error Handling
		if (result) then
			Msg("\nAccount Created\n")
		ApexSQL(pl)
		else
			Msg("\nApex New Player Error!\n")
		end
end

-- Networked values hooked to clients.
function ApexSQL(pl)
	
	-- Variable declairation.
	local sid = pl:SteamID()
	local credits = sql.QueryValue("SELECT credits FROM apex WHERE sid = '"..sid.."'")
	local model = sql.QueryValue("SELECT model FROM apex WHERE sid = '"..sid.."'")
	local name = sql.QueryValue("SELECT name FROM apex WHERE sid = '"..sid.."'")
	local age = sql.QueryValue("SELECT age FROM apex WHERE sid = '"..sid.."'")
	
		pl:SetNWInt("credits", credits)
		pl:SetNWString("model", model)
		pl:SetNWString("name", name)
		pl:SetNWInt("age", age)
	
end

-- Player table updater.
function Save(pl)

	-- Variable declairation.
	local sid = pl:GetNWString("sid")
	local credits = pl:GetNWInt("credits")
	
		sql.Query("UPDATE apex SET credits = "..credits.." WHERE sid = '"..sid.."'")
		Msg("\nAPEX: "..sid.." Saved!\n")

end